package org.rsbot.util;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;

public class StringUtil
{
  private static final String[] COLOURS_STR = { "red", "green", "cyan", "purple", "white" };
  private static final Map<String, Color> COLOR_MAP = new HashMap();

  public static String join(String[] paramArrayOfString) {
    int i = paramArrayOfString.length;
    switch (i) {
    case 0:
      return "";
    case 1:
      return paramArrayOfString[0];
    }

    int j = ", ".length();
    int k = 0;
    for (int m = 0; m < i; m++) {
      k += paramArrayOfString[m].length() + j;
    }
    StringBuffer localStringBuffer = new StringBuffer(k - j);
    m = 0;
    int n = 1;
    while (n != 0) {
      localStringBuffer.append(paramArrayOfString[m]);
      m++;
      n = m < i ? 1 : 0;
      if (n != 0) {
        localStringBuffer.append(", ");
      }
    }
    return localStringBuffer.toString();
  }

  public static void drawLine(Graphics paramGraphics, int paramInt, String paramString)
  {
    FontMetrics localFontMetrics = paramGraphics.getFontMetrics();
    int i = localFontMetrics.getHeight() + 4;
    int j = paramInt * i + 15 + 19;
    String[] arrayOfString1 = paramString.split("\\[");
    int k = 7;
    Color localColor = Color.GREEN;
    for (String str1 : arrayOfString1) {
      for (String str2 : COLOURS_STR)
      {
        int i3 = str1.indexOf(93);
        if (i3 != -1) {
          String str3 = str1.substring(0, i3);
          if (COLOR_MAP.containsKey(str3))
            localColor = (Color)COLOR_MAP.get(str3);
          else
            try {
              Field localField = Color.class.getField(str3);
              int i4 = localField.getModifiers();
              if ((Modifier.isPublic(i4)) && (Modifier.isStatic(i4)) && (Modifier.isFinal(i4))) {
                localColor = (Color)localField.get(null);
                COLOR_MAP.put(str3, localColor);
              }
            }
            catch (Exception localException) {
            }
          str1 = str1.replace(str3 + "]", "");
        }
      }
      paramGraphics.setColor(Color.BLACK);
      paramGraphics.drawString(str1, k, j + 1);
      paramGraphics.setColor(localColor);
      paramGraphics.drawString(str1, k, j);
      k += localFontMetrics.stringWidth(str1);
    }
  }

  public static String throwableToString(Throwable paramThrowable) {
    if (paramThrowable != null) {
      StringWriter localStringWriter = new StringWriter();
      PrintWriter localPrintWriter = new PrintWriter(localStringWriter);
      paramThrowable.printStackTrace(localPrintWriter);
      return localStringWriter.toString();
    }
    return "";
  }
}